﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using BUS;
using DTO;
using TuyenDungViecLam.CommonResource;
namespace TuyenDungViecLam.User_control_VN
{
    public partial class Edit_Job : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                if (int.Parse(Request.QueryString["Edit"]) == 0)
                {
                    LienKetDuLieuEdit();
                }
                else
                {
                    LienKetDuLieu();
                }
            }
        }

        protected void rpnganhnghe_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
            {
                DropDownList dlnganhnghe = (DropDownList)e.Item.FindControl("dlnganhnghe");
                dlnganhnghe.DataSource = NganhNghe_BUS.DanhSachCacNganhNghe();
                dlnganhnghe.DataTextField = "TenNganh";
                dlnganhnghe.DataValueField = "MaNganhNghe";
                dlnganhnghe.DataBind();
                Label lbmanganhnghe = (Label)e.Item.FindControl("lbmanganhnghe");
                for (int i = 0; i < dlnganhnghe.Items.Count; i++)
                {
                    if (dlnganhnghe.Items[i].Value == lbmanganhnghe.Text)
                    {
                        dlnganhnghe.SelectedIndex = i;
                        break;
                    }
                }
            }

        }

        public void LienKetDuLieuEdit()
        {
            string Mcv = Request.QueryString["Mcv"];
            try
            {
                var CTCongViec = ChiTietCongViec_BUS.ChiTietCongViec(Mcv);
                //DVCongNhaTuyenDung.DataSource = CTCongViec; // dung cho nha tuyen dung
                //DVCongNhaTuyenDung.DataBind();

                //DVCongViec.DataSource = CTCongViec; // dung cho nha tuyen dung
                //DVCongViec.DataBind();
                DLCongViec.DataSource = CTCongViec;
                DLCongViec.DataBind();
                lbngayhethandang.Visible = true;
                tbngayhethandang.Visible = false;
                BulletedList listng = (BulletedList)DLCongViec.Items[0].FindControl("blnganhnghe");
                listng.DataSource = NganhNghe_BUS.LayNganhNgheTheoCongViec(Mcv);
                listng.DataTextField = "TenNganh";
                listng.DataBind();
                lbngayhethandang.Text = CTCongViec[0].NgayHetHanDang.ToString();
            }
            catch (Exception)
            {
                Response.Redirect(Request.Url.AbsoluteUri);
            }
        }



        public void LienKetDuLieu()
        {
            string Mcv = Request.QueryString["Mcv"];
            try
            {
                lbngayhethandang.Visible = false;
                tbngayhethandang.Visible = true;
                DLCongViec.EditItemIndex = 0;
                var CTCongViec = ChiTietCongViec_BUS.ChiTietCongViec(Mcv);
                //DVCongNhaTuyenDung.DataSource = CTCongViec; // dung cho nha tuyen dung
                //DVCongNhaTuyenDung.DataBind();

                //DVCongViec.DataSource = CTCongViec; // dung cho nha tuyen dung
                //DVCongViec.DataBind();

                DLCongViec.DataSource = CTCongViec;
                DLCongViec.DataBind();
                Repeater rpnganhnghe = (Repeater)DLCongViec.Items[0].FindControl("rpnganhnghe");
                rpnganhnghe.DataSource = NganhNghe_BUS.LayNganhNgheTheoCongViec(Mcv);
                rpnganhnghe.DataBind();

                DropDownList dlloaihinhcongviec = (DropDownList)DLCongViec.Items[0].FindControl("dlloaihinhcongviec");
                dlloaihinhcongviec.DataSource = ChiTietCongViec_BUS.DanhSachCacLoaiHinhCongViec();
                dlloaihinhcongviec.DataTextField = "TenLoaiHinh";
                dlloaihinhcongviec.DataValueField = "MaLoaiHinhCongViec";
                dlloaihinhcongviec.DataBind();

                Repeater rpnganhnghe2 = (Repeater)DLCongViec.Items[0].FindControl("rpnganhnghe2");
                rpnganhnghe2.DataSource = NganhNghe_BUS.DanhSachCacNganhNghe();
                rpnganhnghe2.DataBind();

                DropDownList dlcapbac = (DropDownList)DLCongViec.Items[0].FindControl("dlcapbac");
                dlcapbac.DataSource = ChiTietCongViec_BUS.DanhSachCacCapBac();
                dlcapbac.DataTextField = "Ten";
                dlcapbac.DataValueField = "MaCapBac";
                dlcapbac.DataBind();
                Label lbmacapbac = (Label)DLCongViec.Items[0].FindControl("lbmacapbac");
                for (int i = 0; i < dlcapbac.Items.Count; i++)
                {
                    if (dlcapbac.Items[i].Value == lbmacapbac.Text)
                    {
                        dlcapbac.SelectedIndex = i;
                        break;
                    }
                }
                Label lbmaloaihinhcongviec = (Label)DLCongViec.Items[0].FindControl("lbmaloaihinhcongviec");
                for (int i = 0; i < dlloaihinhcongviec.Items.Count; i++)
                {
                    if (dlloaihinhcongviec.Items[i].Value == lbmaloaihinhcongviec.Text)
                    {
                        dlloaihinhcongviec.SelectedIndex = i;
                        break;
                    }
                }
                tbngayhethandang.Text = CTCongViec[0].NgayHetHanDang.ToString();
                lbngaydang.Text = CTCongViec[0].NgayDang.ToString();
            }
            catch (Exception)
            {
                Response.Redirect(Request.Url.AbsoluteUri);
            }
        }

        protected void DLCongViec_ItemCommand(object source, DataListCommandEventArgs e)
        {

            //try
            //{
            string url1 = "/NhaTuyenDung/SuaTinTuyenDung.aspx?Mcv=" + Request.QueryString["Mcv"] + "&Mct=" + Request.QueryString["Mct"] + "&Edit=0";
            string url2 = "/NhaTuyenDung/SuaTinTuyenDung.aspx?Mcv=" + Request.QueryString["Mcv"] + "&Mct=" + Request.QueryString["Mct"] + "&Edit=1";
            string url3 = "/NhaTuyenDung/QuanLyTinTuyenDung.aspx?Mct=" + Request.QueryString["Mct"];
            if (e.CommandName == "Cancel")
            {
                if (Session["Role"].ToString() == "NhaTuyenDung")
                    Response.Redirect(url1);
                else
                    Response.Redirect("SuaTin.aspx?Mcv=" + Request.QueryString["Mcv"] + "&Mct=" + Request.QueryString["Mct"] + "&Edit=0");
            }
            if (e.CommandName == "Edit")
            {
                if (Session["Role"].ToString() == "NhaTuyenDung")
                    Response.Redirect(url2);
                else
                    Response.Redirect("SuaTin.aspx?Mcv=" + Request.QueryString["Mcv"] + "&Mct=" + Request.QueryString["Mct"] + "&Edit=1");
            }
            if (e.CommandName == "Update")
            {
                ChiTietCongViec_DTO congviec = new ChiTietCongViec_DTO();
                TextBox tbchucdanh = (TextBox)DLCongViec.Items[0].FindControl("tbchucdanh");
                TextBox tbmotacongviec = (TextBox)DLCongViec.Items[0].FindControl("tbmotacongviec");
                TextBox tbyeucaucongviec = (TextBox)DLCongViec.Items[0].FindControl("tbyeucaucongviec");
                TextBox tbngonngu = (TextBox)DLCongViec.Items[0].FindControl("tbngonngu");
                DropDownList dlcapbac = (DropDownList)DLCongViec.Items[0].FindControl("dlcapbac");
                TextBox tbmucluong = (TextBox)DLCongViec.Items[0].FindControl("tbmucluong");
                TextBox tbluong = (TextBox)DLCongViec.Items[0].FindControl("tbluong");
                TextBox tbsonamkinhnghiem = (TextBox)DLCongViec.Items[0].FindControl("tbsonamkinhnghiem");
                TextBox tbsonkn = (TextBox)DLCongViec.Items[0].FindControl("tbsonkn");
                DropDownList dlloaihinhcongviec = (DropDownList)DLCongViec.Items[0].FindControl("dlloaihinhcongviec");
                Repeater rpnganhnghe = (Repeater)DLCongViec.Items[0].FindControl("rpnganhnghe");
                congviec.NgayHetHanDang = DateTime.Parse(tbngayhethandang.Text);
                congviec.MaCongViec = int.Parse(Request.QueryString["Mcv"]);
                congviec.ChucDanh = tbchucdanh.Text;
                congviec.MoTaCongViec = tbmotacongviec.Text;
                congviec.YeuCauCongViec = tbyeucaucongviec.Text;
                congviec.NgonNgu = tbngonngu.Text;
                congviec.MaCapBac = int.Parse(dlcapbac.SelectedItem.Value);
                congviec.Mucluong = tbmucluong.Text;
                if (tbluong.Text != "")
                    congviec.Luong = int.Parse(tbluong.Text);
                congviec.NamKinhNghiem = tbsonamkinhnghiem.Text;
                if (tbsonkn.Text != "")
                    congviec.SoNamKN = int.Parse(tbsonkn.Text);
                congviec.MaLoaiHinhCongViec = int.Parse(dlloaihinhcongviec.SelectedItem.Value);
                congviec.NgayDang = DateTime.Parse(lbngaydang.Text);
                if (congviec.NgayHetHanDang > congviec.NgayDang)
                {
                    NhaTuyenDung_BUS.CapNhatTinTuyenDung(congviec);
                    for (int i = rpnganhnghe.Items.Count - 1; i >= 0; i--)
                    {
                        DropDownList dlnganhnghe = (DropDownList)rpnganhnghe.Items[i].FindControl("dlnganhnghe");
                        CheckBox cbxoa = (CheckBox)rpnganhnghe.Items[i].FindControl("cbxoa");
                        Label lbmanganhnghe = (Label)rpnganhnghe.Items[i].FindControl("lbmanganhnghe");
                        CONGVIEC_NGANHNGHE congviec_nganhnghe = new CONGVIEC_NGANHNGHE();
                        congviec_nganhnghe.MaNganhNghe = int.Parse(dlnganhnghe.SelectedItem.Value);
                        congviec_nganhnghe.MaCongViec = int.Parse(Request.QueryString["Mcv"]);
                        if (cbxoa.Checked == true)
                        {
                            NganhNghe_BUS.HuyNganhNgheCuaCongViec(congviec_nganhnghe);
                        }
                        else
                        {
                            NganhNghe_BUS.CapNhatNganhNgheCuaCongViec(congviec_nganhnghe, lbmanganhnghe.Text);
                        }
                    }
                    Repeater rpnganhnghe2 = (Repeater)DLCongViec.Items[0].FindControl("rpnganhnghe2");
                    for (int i = 0; i < rpnganhnghe2.Items.Count; i++)
                    {
                        Label lbMng = (Label)rpnganhnghe2.Items[i].FindControl("lbMng");
                        CheckBox cbchon = (CheckBox)rpnganhnghe2.Items[i].FindControl("cbchon");
                        if (cbchon.Checked == true)
                        {
                            CONGVIEC_NGANHNGHE congviec_nganhnghe = new CONGVIEC_NGANHNGHE();
                            congviec_nganhnghe.MaCongViec = int.Parse(Request.QueryString["Mcv"]);
                            congviec_nganhnghe.MaNganhNghe = int.Parse(lbMng.Text);
                            NganhNghe_BUS.ThemCongViec_NganhNghe(congviec_nganhnghe);
                        }
                    }
                    DLCongViec.EditItemIndex = -1;
                    if (Session["Role"].ToString() == "NhaTuyenDung")
                        Response.Redirect(url1);
                    else
                        Response.Redirect("CacTinDang.aspx");
                }
                else
                {
                    Alert.Show("Ngày hết hạn đăng phải lớn hơn ngày " + lbngaydang.Text);
                }

            }
            if (e.CommandName == "Delete")
            {
                ChiTietCongViec_BUS.HuyCongViec(Request.QueryString["Mcv"]);
                Response.Redirect(url3);
            }
            //}
            //catch (Exception ex)
            //{
            //    Response.Redirect(Request.Url.AbsoluteUri);
            //}
        }

        protected void ImageButton3_Click(object sender, ImageClickEventArgs e)
        {
            string url = "QuanLyTinTuyenDung.aspx?Mct=" + Request.QueryString["Mct"];
            Response.Redirect(url);
        }


    }
}